import { Injectable, EventEmitter } from '@angular/core'
import { HttpClient, HttpParams } from '@angular/common/http'
import { ICountryQueryParams, Country } from '@shared/interfaces/common'
import { Observable, throwError, Subject } from 'rxjs'
import { PUBLIC_URL, COMB_URL } from 'app/utils/contraints'
import { map, catchError } from 'rxjs/operators'
import { generateOptions } from 'app/utils/helpers'
import { ObservableStore } from '@shared/classes/ObservableStoreClass'

@Injectable({
  providedIn: 'root',
})
export class CountryService {
  private defaultParams = {
    keyword: '',
    size: 10,
    page: 1,
  }

  public countries$: ObservableStore<Country[]> = new ObservableStore([])

  constructor(private http: HttpClient) {}

  getCounties(params: ICountryQueryParams = this.defaultParams): Observable<Country[]> {
    const url = `${COMB_URL}/camel/public/countries`
    const options = generateOptions(params)
    return this.http.get<Country[]>(url, options).pipe(catchError(err => throwError(err)))
  }
}
